package leetcode.code1424;

import java.util.List;
// 超时
public class Solution {
	// 
	int index;
	int[] ans;
	int max;

	public int[] findDiagonalOrder(List<List<Integer>> nums) {
		int size = 0;
		for (int i = 0; i < nums.size(); i++) {
			int cur = nums.get(i).size();
			size += cur;
			max = Math.max(max, cur);
		}
		ans = new int[size];
		index = 0;
		for (int i = 0; i < nums.size(); i++) {
			this.f(i, 0, nums);
		}
		for (int i = 1; i < max; i++) {
			this.f(nums.size() - 1, i, nums);
		}
		return ans;
	}

	private void f(int r, int c, List<List<Integer>> nums) {
		if (r == -1 || c == max) {
			return;
		}
		if (nums.get(r).size() > c) {
			ans[index++] = nums.get(r).get(c);
		}
		this.f(r - 1, c + 1, nums);
	}
}
